package com.origin.grid.buy.controller.distribution;

import com.origin.grid.framework.common.aop.annotation.PreventDuplicateSubmissions;
import com.origin.grid.framework.common.enums.ResultCode;
import com.origin.grid.framework.common.enums.ResultUtil;
import com.origin.grid.framework.common.exception.ServiceException;
import com.origin.grid.framework.common.vo.ResultMessage;
import com.origin.grid.framework.modules.distribution.entity.dto.DistributionGoodsSearchParams;
import com.origin.grid.framework.modules.distribution.entity.vos.DistributionGoodsVO;
import com.origin.grid.framework.modules.distribution.service.DistributionGoodsService;
import com.origin.grid.framework.modules.distribution.service.DistributionSelectedGoodsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.constraints.NotNull;

/**
 * 买家端,分销商品接口
 *
 * @author Bulbasaur
 * @since 2020/11/16 10:06 下午
 */
@RestController
@Schema(name = "买家端,分销商品接口")
@RequestMapping("/buyer/distribution/goods")
public class DistributionGoodsBuyerController {

    /**
     * 分销商品
     */
    @Autowired
    private DistributionGoodsService distributionGoodsService;
    /**
     * 选择分销商品
     */
    @Autowired
    private DistributionSelectedGoodsService distributionSelectedGoodsService;


    @Schema(name = "获取分销商商品列表")
    @GetMapping
    public ResultMessage<IPage<DistributionGoodsVO>> distributionGoods(DistributionGoodsSearchParams distributionGoodsSearchParams) {
        return ResultUtil.data(distributionGoodsService.goodsPage(distributionGoodsSearchParams));
    }

    @PreventDuplicateSubmissions
    @Schema(name = "选择分销商品")
    @Parameters({
           @Parameter(name = "distributionGoodsId", description = "分销ID", required = true, schema = @Schema(type = "String"), in = ParameterIn.PATH),
           @Parameter(name = "checked", description = "是否选择", required = true, in = ParameterIn.QUERY)
    })
    @GetMapping(value = "/checked/{distributionGoodsId}")
    public ResultMessage<Object> distributionCheckGoods(
            @NotNull(message = "分销商品不能为空") @PathVariable("distributionGoodsId") String distributionGoodsId, Boolean checked) {
        Boolean result = false;
        if (checked) {
            result = distributionSelectedGoodsService.add(distributionGoodsId);
        } else {
            result = distributionSelectedGoodsService.delete(distributionGoodsId);
        }
        //判断操作结果
        if (result) {
            return ResultUtil.success(ResultCode.SUCCESS);
        } else {
            throw new ServiceException(ResultCode.ERROR);
        }

    }
}
